//
//  PersonalViewController.m
//  Tongcheng
//
//  Created by   丛熠超 on 2017/4/7.
//  Copyright © 2017年 Tongcheng. All rights reserved.
//个人信息界面

#import "PersonalViewController.h"
#import "PersonalTableViewCell.h"
#import "MineOperator.h"
#import "MineUserinfoModel.h"
#import "MineUserinfoDataModel.h"
#import "FocusOperator.h"
#import "PersonalModel.h"
#import "TipVIew.h"
#import "MineBuyYBViewController.h"
#import "UIImageView+AFNetworking.h"

@interface PersonalViewController ()<UITableViewDelegate, UITableViewDataSource,TipViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *IconImageView;
@property (weak, nonatomic) IBOutlet UITableView *PersonalVIew;
@property (weak, nonatomic) IBOutlet UIView *HeaderView;
@property (weak, nonatomic) IBOutlet UILabel *IDLB;
@property (weak, nonatomic) IBOutlet UILabel *NameLB;
@property (weak, nonatomic) IBOutlet UILabel *CityLB;
@property (weak, nonatomic) IBOutlet UIButton *DistantLB;
@property (weak, nonatomic) IBOutlet UIButton *HotLB;
@property (weak, nonatomic) IBOutlet UIButton *PhotpBtn;
@property (weak, nonatomic) IBOutlet UIButton *CareBtn;
@property (weak, nonatomic) IBOutlet UIButton *WeiXinBtn;
@property (nonatomic, strong)TipVIew *tip;

@property (nonatomic, strong) MineUserinfoDataModel *dataModel;

@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation PersonalViewController

static NSString *mainCellIdentifier = @"personaltableviewcell";

- (void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    [_PersonalVIew reloadData];
}

- (void)setupHeaderView
{
    [self.IconImageView setImageWithURL:[NSURL URLWithString:self.dataModel.avatar] placeholderImage:nil];
    self.IDLB.text = [NSString stringWithFormat:@"ID:%@", self.dataModel._id];
    self.NameLB.text = self.dataModel.user_nicename;
    self.CityLB.text = self.dataModel.cityid;
}

- (void)setupNavigation
{
    self.navigationItem.title = self.navTitle;
}

- (void)setupView
{
    _PersonalVIew.delegate = self;
    _PersonalVIew.dataSource = self;
    _PersonalVIew.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavigation];
    [self setupView];
    [self download];
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)SendMessage:(id)sender {
    NSLog(@"send");
}

- (IBAction)SayHi:(id)sender {
    NSLog(@"sayhi");
}
- (IBAction)CareYou:(id)sender {
    //添加关注
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"token"] = self.config.token;
    dic[@"user_id"] = self.config.userId;
    dic[@"follow_uid"] = self.userId;
    FocusOperator *operator = [[FocusOperator alloc] initWithParams:dic];
    [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
        if ([baseModel.code isEqualToString:@"1"]) {
            [self showToastMessage:baseModel.msg];
        }
    } failBlock:^(BaseModel *baseModel) {
        
    }];
}

- (IBAction)CheckPhoto:(id)sender {
    _tip = [TipVIew gainMineHeaderView];
    _tip.delegate = self;
    [self.view addSubview:_tip];
}
- (void)GoBuyVip {
    MineBuyYBViewController *vb = [[MineBuyYBViewController alloc] init];
    [_tip removeFromSuperview];
    [self.navigationController pushViewController:vb animated:YES];
    
}

- (IBAction)CareBtn:(id)sender {
    NSLog(@"guanzhu");
}
- (IBAction)GetWeixin:(id)sender {
    NSLog(@"weixin");
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PersonalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mainCellIdentifier];
    if (!cell)
    {
        cell = [[UINib nibWithNibName:@"PersonalTableViewCell" bundle:nil] instantiateWithOwner:self options:nil].lastObject;
    }
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            PersonalModel *model = self.dataArray[0];
            cell.model = model;
        }else if (indexPath.row == 1){
            PersonalModel *model = self.dataArray[1];
            cell.model = model;
        }else if (indexPath.row == 2){
            PersonalModel *model = self.dataArray[2];
            cell.model = model;
        }else if (indexPath.row == 3){
            PersonalModel *model = self.dataArray[3];
            cell.model = model;
        }else{
            PersonalModel *model = self.dataArray[4];
            cell.model = model;
        }
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            PersonalModel *model = self.dataArray[5];
            cell.model = model;
        }else if (indexPath.row == 1){
            PersonalModel *model = self.dataArray[6];
            cell.model = model;
        }else if (indexPath.row == 2){
            PersonalModel *model = self.dataArray[7];
            cell.model = model;
        }else if (indexPath.row == 3){
            PersonalModel *model = self.dataArray[8];
            cell.model = model;
        }else if (indexPath.row == 3){
            PersonalModel *model = self.dataArray[9];
            cell.model = model;
        }else if (indexPath.row == 3){
            PersonalModel *model = self.dataArray[10];
            cell.model = model;
        }else{
            PersonalModel *model = self.dataArray[11];
            cell.model = model;
        }
    }else if (indexPath.section == 2){
        if (indexPath.row == 0) {
            PersonalModel *model = self.dataArray[12];
            cell.model = model;
        }else if (indexPath.row == 1){
            PersonalModel *model = self.dataArray[13];
            cell.model = model;
        }else{
            PersonalModel *model = self.dataArray[14];
            cell.model = model;
        }
    }else{
        if (indexPath.row == 0) {
            PersonalModel *model = self.dataArray[15];
            cell.model = model;
        }else if (indexPath.row == 1){
            PersonalModel *model = self.dataArray[16];
            cell.model = model;
        }else if (indexPath.row == 2){
            PersonalModel *model = self.dataArray[17];
            cell.model = model;
        }else if (indexPath.row == 3){
            PersonalModel *model = self.dataArray[18];
            cell.model = model;
        }else{
            PersonalModel *model = self.dataArray[19];
            cell.model = model;
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 5;
    } else
    if (section == 1) {
        return 7;
    } else
    if (section == 2) {
        return 3;
    } else
    if (section == 3) {
        return 5;
    }else {
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 60)];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    line.backgroundColor = RGB(0xf2f0f1);
    [sectionView addSubview:line];
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, 10, kScreenWidth, 1)];
    line2.backgroundColor = RGB(0xD2d2d2);
    [sectionView addSubview:line2];
    
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(25, 10, kScreenWidth, 50)];
    name.font = [UIFont systemFontOfSize:17.0];
    if (section == 0) {
        name.text = @"个人空间";
    }
    if (section == 1) {
        name.text = @"基本资料";
    }
    if (section == 2) {
        name.text = @"联系方式";
    }
    if (section == 3) {
        name.text = @"详细资料";
    }
    UIView *line3 = [[UIView alloc] initWithFrame:CGRectMake(0, 60, kScreenWidth, 1)];
    line3.backgroundColor = RGB(0xD2d2d2);
    [sectionView addSubview:line3];
    [sectionView addSubview:name];
    sectionView.backgroundColor = [UIColor whiteColor];
    return sectionView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 60;
}

- (void)download
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"token"] = self.config.token;
    dic[@"user_id"] = self.config.userId;
    dic[@"uid"] = self.userId;
    MineOperator *operator = [[MineOperator alloc] initWithParams:dic];
    [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
        if ([baseModel.code isEqualToString:@"1"]) {
            self.dataModel = operator.data.data;
            //重组数组
            [self setupDataWith:self.dataModel];
            [self setupHeaderView];
        }
    } failBlock:^(BaseModel *baseModel) {
        
    }];
}

- (void)setupDataWith:(MineUserinfoDataModel *)model
{
    NSMutableArray *array = [NSMutableArray array];
    PersonalModel *model1 = [[PersonalModel alloc] initPersonalModelWithTitle:@"最近上线时间" withData:model.last_login_time];
    [array addObject:model1];
    
    PersonalModel *model2 = [[PersonalModel alloc] initPersonalModelWithTitle:@"交友目的" withData:model.married_text];
    [array addObject:model2];
    
    PersonalModel *model3 = [[PersonalModel alloc] initPersonalModelWithTitle:@"恋爱观念" withData:model.outlook_text];
    [array addObject:model3];
    
    PersonalModel *model4 = [[PersonalModel alloc] initPersonalModelWithTitle:@"首次见面希望" withData:model.hoping];
    [array addObject:model4];
    
    PersonalModel *model5 = [[PersonalModel alloc] initPersonalModelWithTitle:@"喜欢约会的地点" withData:model.dating_add];
    [array addObject:model5];
    
    PersonalModel *model6 = [[PersonalModel alloc] initPersonalModelWithTitle:@"昵称" withData:model.user_nicename];
    [array addObject:model6];
    
    PersonalModel *model7 = [[PersonalModel alloc] initPersonalModelWithTitle:@"性别" withData:model.sex];
    [array addObject:model7];
    
    PersonalModel *model8 = [[PersonalModel alloc] initPersonalModelWithTitle:@"年龄" withData:model.age];
    [array addObject:model8];
    
    PersonalModel *model9 = [[PersonalModel alloc] initPersonalModelWithTitle:@"居住地" withData:model.cityid];
    [array addObject:model9];
    
    PersonalModel *model10 = [[PersonalModel alloc] initPersonalModelWithTitle:@"身高" withData:model.height];
    [array addObject:model10];
    
    PersonalModel *model11 = [[PersonalModel alloc] initPersonalModelWithTitle:@"月收入" withData:model.income];
    [array addObject:model11];
    
    PersonalModel *model12 = [[PersonalModel alloc] initPersonalModelWithTitle:@"婚姻状态" withData:model.married_text];
    [array addObject:model12];
    
    PersonalModel *model13 = [[PersonalModel alloc] initPersonalModelWithTitle:@"QQ" withData:model.qq];
    [array addObject:model13];
    
    PersonalModel *model14 = [[PersonalModel alloc] initPersonalModelWithTitle:@"手机" withData:model.phone];
    [array addObject:model14];
    
    PersonalModel *model15 = [[PersonalModel alloc] initPersonalModelWithTitle:@"微信" withData:model.weixin];
    [array addObject:model15];
    
    PersonalModel *model16 = [[PersonalModel alloc] initPersonalModelWithTitle:@"学历" withData:model.edu];
    [array addObject:model16];
    
    PersonalModel *model17 = [[PersonalModel alloc] initPersonalModelWithTitle:@"职业" withData:model.job];
    [array addObject:model17];
    
    PersonalModel *model18 = [[PersonalModel alloc] initPersonalModelWithTitle:@"生日" withData:model.birthday];
    [array addObject:model18];
    
    PersonalModel *model19 = [[PersonalModel alloc] initPersonalModelWithTitle:@"体重" withData:@"48kg"];
    [array addObject:model19];
    
    PersonalModel *model20 = [[PersonalModel alloc] initPersonalModelWithTitle:@"星座" withData:model.astro];
    [array addObject:model20];
    
    self.dataArray = [array copy];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
